dicts module
Module containing functions that binary_c-python uses to modify dictionaries.
- class binarycpython.utils.dicts.AutoVivificationDict[source]
Bases:
dict
Implementation of perl’s autovivification feature, by overriding the get item and the __iadd__ operator (https://docs.python.org/3/reference/datamodel.html?highlight=iadd#object.__iadd__)
This allows to set values within a subdict that might not exist yet:
Example
newdict = {} newdict[‘example’][‘mass’] += 10 print(newdict) >>> {‘example’: {‘mass’: 10}}
- binarycpython.utils.dicts.count_keys_recursive(input_dict)[source]
Function to recursively count the total number of keys in a dictionary.
- Parameters
input_dict (
dict
) – dictionary that we want to know the total amount of keys from.- Returns
total amount of keys within the input_dict.
- Return type
local_count
- binarycpython.utils.dicts.custom_sort_dict(input_dict)[source]
Returns a dictionary that is ordered, but can handle numbers better than normal OrderedDict
When the keys of the current dictionary are of mixed type, we first find all the unique types. Sort that list of type names. Then find the values that fit that type. Sort those and append them to the sorted keys list. This is done until all the keys are sorted.
All objects other than dictionary types are directly return as they are
- Parameters
input_dict (
dict
) – object which will be sorted (and returned as a new object) if its a dictionary, otherwise it will be returned without change.- Return type
dict
- binarycpython.utils.dicts.filter_dict(arg_dict, filter_list)[source]
Function to filter out keys that are contains in filter_list
- Parameters
arg_dict (
dict
) – dictionary containing the argument + default key pairs of binary_cfilter_list (
list
) – lists of keys to be filtered out
- Return type
dict
- Returns
filtered dictionary
- binarycpython.utils.dicts.filter_dict_through_values(arg_dict, filter_list)[source]
Function to filter out keys that contain values included in filter_list
- Parameters
arg_dict (
dict
) – dictionary containing the argument + default key pairs of binary_cfilter_list (
list
) – lists of values to be filtered out
- Return type
dict
- Returns
filtered dictionary
- binarycpython.utils.dicts.inspect_dict(input_dict, indent=0, print_structure=True)[source]
Function to (recursively) inspect a (nested) dictionary. The object that is returned is a dictionary containing the key of the input_dict, but as value it will return the type of what the value would be in the input_dict
In this way we inspect the structure of these dictionaries, rather than the exact contents.
- Parameters
input_dict (
dict
) – dictionary you want to inspectprint_structure (
bool
) – (optional, default = True)indent (
int
) – (optional, default = 0) indent of the first output
- Return type
dict
- Returns
- Dictionary that has the same structure as the input_dict, but as values it has the
type(input_dict[key]) (except if the value is a dict)
- binarycpython.utils.dicts.keys_to_floats(input_dict)[source]
Function to convert all the keys of the dictionary to float to float
- we need to convert keys to floats:
this is ~ a factor 10 faster than David’s
recursive_change_key_to_float
routine, probably because this version only does the float conversion, nothing else.
- Parameters
input_dict (
dict
) – dict of which we want to turn all the keys to float types if possible- Returns
dict of which the keys have been turned to float types where possible
- Return type
new_dict
- binarycpython.utils.dicts.merge_dicts(dict_1, dict_2)[source]
Function to merge two dictionaries in a custom way.
Behaviour:
- When dict keys are only present in one of either:
we just add the content to the new dict
- When dict keys are present in both, we decide based on the value types how to combine them:
dictionaries will be merged by calling recursively calling this function again
numbers will be added
(opt) lists will be appended
booleans are merged with logical OR
identical strings are just set to the string
non-identical strings are concatenated
NoneTypes are set to None
In the case that the instances do not match: for now I will raise an error
- Parameters
dict_1 (
dict
) – first dictionarydict_2 (
dict
) – second dictionary
- Return type
dict
- Returns
Merged dictionary
- binarycpython.utils.dicts.multiply_float_values(input_dict, factor, ignore=None)[source]
A function to recursively multiply values of a (nested) dictionary that are floats by a constant. Nested dictionaries call this function recursively.
- Parameters
input_dict – the dictionary
factor – the constant that multiplies float values
- binarycpython.utils.dicts.multiply_values_dict(input_dict, factor)[source]
Function that goes over dictionary recursively and multiplies the value if possible by a factor
If the key equals “general_info”, the multiplication gets skipped.
This function changes the values in-place, so the original dict is modified
- Parameters
input_dict (
dict
) – dictionary of which we want to multiply the values by <factor>factor (
Union
[int
,float
,complex
,number
]) – factor that we want to multiply the values with
- Returns
dict containing the multiplied keys. This is the same object as we passed as input.
- Return type
multiplied_dict
- binarycpython.utils.dicts.normalize_dict(result_dict)[source]
Function to normalise a dictionary by summing all the values and dividing each term by the total. Designed for dictionary containing only positive values.
- Parameters
result_dict (
dict
) – dictionary where values should be positive number objects- Returns
dictionary where the values are normalised to sum to 1
- Return type
normalized_dict
- binarycpython.utils.dicts.prepare_dict(global_dict, list_of_sub_keys)[source]
Function that makes sure that the global dict is prepared to have a value set there. This dictionary will store values and factors for the distribution functions, so that they don’t have to be calculated each time.
- Parameters
global_dict (
dict
) – globally accessible dictionary where factors are stored inlist_of_sub_keys (
list
) – List of keys that must become be(come) present in the global_dict
- Return type
None
- binarycpython.utils.dicts.recursive_change_key_to_float(input_dict)[source]
Function to recursively change the key to float
This only works if the dict contains just sub-dicts or numbers/strings.
Does not work with lists as values
- Parameters
input_dict (
dict
) – dict of which we want to turn all the keys to float types if possible- Returns
dict of which the keys have been turned to float types where possible
- Return type
new_dict
If input_dict is None or empty, returns an empty dict
- binarycpython.utils.dicts.recursive_change_key_to_string(input_dict, custom_format='{:g}')[source]
Function to recursively change the key back to a string but this time in a format that we decide. We’ll try to turn a string key into a float key before formatting the key
- Parameters
input_dict (
dict
) – dict of which we want to turn all the keys to string types (with a custom format)custom_format (
str
) – custom format used when turning the key to strings
- Returns
dict of which the keys have been turned to string types where possible
- Return type
new_dict
- binarycpython.utils.dicts.set_opts(opts, newopts)[source]
Function to take a default dict and override it with newer values.
# TODO: consider changing this to just a dict.update
- Parameters
opts (
dict
) – dictionary with default valuesnewopts (
dict
) – dictionary with new values
- Return type
dict
- Returns
returns an updated dictionary
- binarycpython.utils.dicts.subtract_dicts(dict_1, dict_2)[source]
Function to subtract two dictionaries, i.e.
dict_1 - dict_2
Only allows values to be either a dict or a numerical type
- For the overlapping keys (key name present in both dicts):
When the keys are of the same type: If the types are of numerical type we subtract the value at dict 2 from dict 1. If the types are both dictionaries: call this function with the subdicts
When the keys are not of the same type: If the keys are all of numerical types we do the subtraction. If they are not numerical we raise an error.
- For the unique keys:
If the key is from dict 1: adds the value to the new dict (be it numerical value or dict)
If the key is from dict 2: Adds the negative of its value in case of numerical type. If the type is a dict, the result of
subtract_dicts({}, dict_2[key])
will be set
If the result is 0, the key will be removed from the resulting dict.
If that results in an empty dict, the dict will be removed too.
- Parameters
dict_1 (
dict
) – first dictionarydict_2 (
dict
) – second dictionary
- Return type
dict
- Returns
Subtracted dictionary, i.e.
dict_1 - dict_2
- binarycpython.utils.dicts.update_dicts(dict_1, dict_2)[source]
Function to update dict_1 with values of dict_2 in a recursive way.
- Behaviour:
When dict keys are only present in one of either: we just add the content to the new dict
When dict keys are present in both, we decide based on the value types how to combine them: value of dict2 will be taken
- Parameters
dict_1 (
dict
) – first dictionarydict_2 (
dict
) – second dictionary
- Return type
dict
- Returns
New dictionary with Updated values